package vista;

import co.edu.icesi.tv.game.ImageDictionary;
import vista.interfaces.EventDispatcher;
import vista.modelo.VNode;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class WindowLayer {

	public static final int GRID = 30;

	private int width, height;
	public boolean leftDrag, rigthDrag;
	private boolean optionsMenu;
	private EventDispatcher eventDispatcher;
	private VNode editNode;
	private ImageDictionary resources;

	// Buttons positions
	private final Rect optionsMenuRegion, reflexiveButton, editNodeButton, removeButton;

	public WindowLayer(int width, int height, EventDispatcher eventDispatcher, ImageDictionary resources) {
		this.width = width;
		this.height = height;
		this.eventDispatcher = eventDispatcher;
		this.resources = resources;

		// Build buttons
		int size = width/4;
		optionsMenuRegion = new Rect(0, 0, width, size);
		reflexiveButton = new Rect(10, 10, size-20, size-20);
		editNodeButton = new Rect(size+10, 10, size*2-20, size-20);
		removeButton = new Rect(size*2+10, 10, size*3-20, size-20);
	}

	public boolean guessDragging(int xAbsolute) {
		if(xAbsolute < GRID) {
			leftDrag = true;
			return true;
		} else if(!leftDrag && xAbsolute > width - GRID) {
			rigthDrag = true;
			return true;
		}
		return false;
	}
	
	public boolean isOptionMenuOpen() {
		return optionsMenu;
	}

	public void setUpOptionMenu(VNode selected) {
		optionsMenu = true;
		editNode = selected;
	}

	public void setDownOptionMenu() {
		optionsMenu = false;
		editNode = null;
	}

	public boolean collide(int x, int y) {
		if(optionsMenu) {
			if(reflexiveButton.contains(x, y)) {
				eventDispatcher.dispatchEvent(new Object[]{editNode}, InterfazControl.REFLEXIVE_RELATION);
				optionsMenu = false;
			} else if(editNodeButton.contains(x, y)) {
				eventDispatcher.dispatchEvent(new Object[]{editNode}, InterfazControl.EDIT_NODE);
				optionsMenu = false;
			}
//			else if(removeButton.contains(x, y)) {
//			}
				optionsMenu = optionsMenuRegion.contains(x, y);
				return optionsMenu;
		} else {
			editNode = null;
			return false;
		}
	}

	public void drawUpperLayer(Canvas canvas, Paint paint, int xAbsolute) {

		// Paint configuration
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(0xAA000000);

		// Options menu layer
		if(optionsMenu) {
			canvas.drawRect(optionsMenuRegion, paint);
//			canvas.drawRect(reflexiveButton, paint);
			canvas.drawBitmap(resources.getImage(2), null, reflexiveButton, null);
//			canvas.drawRect(editNodeButton, paint);
			canvas.drawBitmap(resources.getImage(0), null, editNodeButton, null);
//			canvas.drawRect(removeButton, paint);
//			canvas.drawBitmap(resources.getImage(3), null, removeButton, null);
		}

		// Dragging layer
		if(xAbsolute != 0) {
			if(leftDrag)
				canvas.drawRect(0, 0, xAbsolute, height, paint);
			else if(rigthDrag)
				canvas.drawRect(xAbsolute, 0, width, height, paint);
		}
	}

	public void drawLowerLayer(Canvas canvas, Paint paint, int xWindow, int yWindow) {

		// Grid layer
		paint.setColor(Color.LTGRAY);
		for (int x = -(xWindow%GRID); x < width; x += GRID)
			canvas.drawLine(x, 0, x, height, paint);
		for (int y = -(yWindow%GRID); y < height; y += GRID)
			canvas.drawLine(0, y, width, y, paint);
	}

}
